﻿using DG.Tweening.Plugins.Core.PathCore;
using Miao;
using NAudio.Wave;
using RuntimeAudioClipLoader;
using Sirenix.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Mathematics;
using UnityEngine;

public class ModContentSound : ModContentHolder<AudioClip>
{
    public override string Folder => "Sounds\\";

    public ModContentSound(ModContentPack mod) : base(mod)
    {

    }



    public override void AddFile(FileInfo file)
    {

        FileStream fileStream = file.OpenRead();
        CustomAudioFileReader reader = new CustomAudioFileReader(fileStream,file.Extension);
        WaveFormat waveFormat = reader.WaveFormat;
        int samplesCount = (int)reader.Length / (waveFormat.BitsPerSample / 8);


       AudioClip audioClip = AudioClip.Create(file.Name, samplesCount, waveFormat.Channels, waveFormat.SampleRate, false, (float[] target) =>
        {
            reader.Read(target, 0, target.Length);
        },
        (int target) =>
        {
            reader.Seek(target, SeekOrigin.Begin);
        }
        );
        reader.Dispose();//疑似读取后直接释放也可以

        string path = GetSubPath(file);
        ContentDict.Add(path, audioClip);
        //float[] data = new float[samplesCount];
        //reader.Read(data, 0, data.Length);
        //audioClip.SetData(data, 0);


        //if (file.Extension == ".wav")
        //{
        //    string path = GetSubPath(file);
        //    FileStream fileStream = file.OpenRead();
        //    //byte[] buffer = new byte[fileStream.Length];
        //    //fileStream.Read(buffer, 0, buffer.Length);
        //    //AudioClip audioClip = AudioLoadReader.Load(fileStream, file.Name, AudioType.WAV);

        //    //ContentDict.Add(path, audioClip);

        //    //texture2D.LoadImage(buffer);
        //    //texture2D.name = Path.GetFileNameWithoutExtension(file.Name);

        //}
    }
}

